/**********************************************************
 * AccountingManagerAction.java
 * Created by ankur
 * Last modified Date: Aug 5, 2008 11:36:37 AM by ankur
 * Version : 0.1
 * Copyright (c) 2008 - 2008 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.Preparable;
import com.printeverywhere.delegator.CreditCardServiceDelegator;
import com.printeverywhere.delegator.CreditCardTypeServiceDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.CreditCardType;
import com.printeverywhere.dto.Creditcard;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SessionUtil;

/**
 * The Class AccountingManagerAction.
 */
public class CreditCardAction extends FrontBaseSupport implements Preparable,
		ServletRequestAware {

	public CreditCardAction() {
		super(true, false);
		setMyAccount(true);
	}

	/** Logger for this class. */
	private static final Logger logger = Logger
			.getLogger(CreditCardAction.class);
	private SearchCriteria searchCriteria = new SearchCriteria();

	// @Override
	public void prepare() throws Exception {
		user = SessionUtil.getUser(request);
		creditCardTypeList = CreditCardTypeServiceDelegator.getList();
		searchCriteria.removeAllAttribute();
		searchCriteria.setAttribute(AttributeConstants.USER, SessionUtil
				.getUser(request));
		creditCardList = CreditCardServiceDelegator.getList(searchCriteria);
	}

	/**
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
		}
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end");
		}
		return SUCCESS;
	}

	/**
	 * Adds the credit card.
	 *
	 * @return the string
	 *
	 * @throws Exception
	 *             the exception
	 */
	public String addCreditCard() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addCreditCard() - start");
		}

		if (creditcard != null && creditcard.getCreaditCardId() != null) {
			creditcard = CreditCardServiceDelegator.getCreditcard(
					this.creditcard.getCreaditCardId(), SessionUtil
							.getUser(request));
		}

		if (logger.isDebugEnabled()) {
			logger.debug("addCreditCard() - end");
		}

		return SUCCESS;
	}

	/**
	 * Delete credit card.
	 *
	 * @return the string
	 *
	 * @throws Exception
	 *             the exception
	 */
	public String deleteCreditCard() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteCreditCard() - start"); //$NON-NLS-1$
		}
		try {
			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute(AttributeConstants.CREADITCARDID,
					defaultCardId);
			creditcard = CreditCardServiceDelegator.getList(searchCriteria)
					.get(AdminConstants.ZERO_NUMBER);
			CreditCardServiceDelegator.deleteCreditcard(creditcard);
		} catch (Exception e) {
			addActionError("Error in deleting credit card...".intern());
		}
		if (logger.isDebugEnabled()) {
			logger.debug("deleteCreditCard() - end"); //$NON-NLS-1$
		}

		return SUCCESS;
	}

	/**
	 * Default credit card.
	 *
	 * @return the string
	 *
	 * @throws Exception
	 *             the exception
	 */
	public String defaultCreditCard() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("defaultCreditCard() - start"); //$NON-NLS-1$
		}

		user = SessionUtil.getUser(request);
		user.setCreditcard(CreditCardServiceDelegator.getCreditcard(
				defaultCardId, user));
		UserServiceDelegator.updateUser(user);
		request.getSession().setAttribute(AttributeConstants.USER, user);
		if (logger.isDebugEnabled()) {
			logger.debug("defaultCreditCard() - end"); //$NON-NLS-1$
		}

		return SUCCESS;
	}

	public String addCreditCardSubmit() throws Exception {
		creditcard.setUser(SessionUtil.getUser(request));
		CreditCardServiceDelegator.insertCreditcard(creditcard);
		return SUCCESS;
	}

	private HttpServletRequest request;

	private Creditcard creditcard;
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	private List<Creditcard> creditCardList = new ArrayList<Creditcard>();
	private List<CreditCardType> creditCardTypeList;
	private String button;
	private User user;
	private int defaultCardId;

	/**
	 * @return the defaultCardId
	 */
	public int getDefaultCardId() {
		return defaultCardId;
	}

	/**
	 * @param defaultCardId
	 *            the defaultCardId to set
	 */
	public void setDefaultCardId(int defaultCardId) {
		this.defaultCardId = defaultCardId;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the button
	 */
	public String getButton() {
		return button;
	}

	/**
	 * @param button
	 *            the button to set
	 */
	public void setButton(String button) {
		this.button = button;
	}

	/**
	 * @return the creditCardTypeList
	 */
	public List<CreditCardType> getCreditCardTypeList() {
		return creditCardTypeList;
	}

	/**
	 * @param creditCardTypeList
	 *            the creditCardTypeList to set
	 */
	public void setCreditCardTypeList(List<CreditCardType> creditCardTypeList) {
		this.creditCardTypeList = creditCardTypeList;
	}

	/**
	 * @return the creditcard
	 */
	public Creditcard getCreditcard() {
		return creditcard;
	}

	/**
	 * @param creditcard
	 *            the creditcard to set
	 */
	public void setCreditcard(Creditcard creditcard) {
		this.creditcard = creditcard;
	}

	// @Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;

	}

	/**
	 * @return the creditCardList
	 */
	public List<Creditcard> getCreditCardList() {
		return creditCardList;
	}

	/**
	 * @param creditCardList
	 *            the creditCardList to set
	 */
	public void setCreditCardList(List<Creditcard> creditCardList) {
		this.creditCardList = creditCardList;
	}

}